package singleton.type4;

/**
 * @author 龙超
 * @date 2020/7/1 15:27
 * @Description 使用线程不安全的懒汉模式
 */
public class Singleton4 {
    public static void main(String[] args) {
        System.out.println("使用线程不安全的懒汉式");
        Singleton instance = Singleton.getInstance();
        Singleton instance1 = Singleton.getInstance();
        System.out.println(instance==instance1);//不一定恒为true了
        System.out.println(instance.hashCode());
        System.out.println(instance1.hashCode());
    }
}

class Singleton {
    private static Singleton instance;

    private Singleton() {
    }

    //使用同步方法
    public static synchronized Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}
